<template>
	<view class="distribution-posters">
		<view class="slider-banner banner">
			<swiper indicatorDots="true">
				<block v-for="(item, infoIndex) in info" :key="infoIndex">
					<swiper-item style="overflow-y: auto;">
						<image class="slide-image" :src="item.wap_poster" mode="widthFix" show-menu-by-longpress />
					</swiper-item>
				</block>
			</swiper>
		</view>
		<view class="footer_btn">
			<view class="item">
				<button type="default" open-type='share'>分享给朋友</button>
			</view>
			<view class="item">
				<button type="default" @click="goRoom">观看直播</button>
			</view>

		</view>
	</view>
</template>
<script>
	let HAS_LIVE = false
	// #ifdef MP-WEIXIN
	// HAS_LIVE = true
	// let livePlayer = null;
	// if (HAS_LIVE) {
	// 	livePlayer = requirePlugin('live-player-plugin');
	// }
	//  #endif
	import {
		yxWechatLive,
		getLiveReplay
	} from '@/api/live';
	import {
		getLivePoster
	} from "@/api/user";
	import {
		isWeixin,
		parseLiveQueryItem,
		handleUrlParam
	} from "@/utils";
	import cookie from "@/utils/store/cookie";
	export default {
		name: "Poster",
		components: {},
		data: function() {
			return {
				info: [],
				roomId: '',
				coverImge: '',
				activeIndex: 0,
				userInfo: {},
				disableLive: []
			};
		},
		mounted: function() {
			uni.showLoading({
				title: "加载中"
			});
			this.userInfo = this.$store.getters.userInfo
			this.disableLive = this.$store.getters.disableLive
			console.log('==================')
			console.log(this.userInfo)
			console.log(this.disableLive)
			this.roomId = this._route.query.roomId;
			this.coverImge = this._route.query.coverImge
			if (this._route.query.roomId) {
				this.roomId = this._route.query.roomId;
				this.coverImge = this._route.query.coverImge
			} else {
				
				let url = parseLiveQueryItem();
				if (!url) {
					url = handleUrlParam(getCurrentPageUrlWithArgs())
				}
				if (url) {
			
					var scene = decodeURIComponent(url.scene)
			
					const part = scene.split('-');
					const parts = part[1].split('=');
					const id = parts[1];
					this.roomId = id;
				} else {
					this.roomId = this._route.query.roomId;
					this.coverImge = this._route.query.coverImge
				}
			}
			
			if(this._route.query.spread){
				let urlSpread = this._route.query.spread
				cookie.set("spread", urlSpread)
			}

			if (!this.$store.getters.token) {
				this.$yrouter.reLaunch({
					path: "/pages/authorization/index"
				});
			}
			this.getIndex();
			let that = this;

		},
		computed: {
			swiper() {
				
			}
		},
		methods: {

			goRoom() {
				let that = this
				let level = that.userInfo.level + ''
				console.log(level)
				console.log(that.disableLive)
				if(that.disableLive.includes(level)){
					wx.navigateTo({
						url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${that.roomId}`
					});
				}else{
					uni.showToast({
						title: '您的等级暂无权限观看直播',
						icon: "none",
						duration: 2000
					});
				}
			},
			
			onShareAppMessage: function() {
				return {
					title: this.miniHomeRemark,
					imageUrl: this.miniHomeImg,
					path: "pages/LivePosters/LivePoster?spread=" + uni.getStorageSync("uid") + '&roomId=' + this.roomId,
				};
			},
			getIndex: function() {
				let that = this;
				getLivePoster(that.roomId,{
					from: this.$deviceType == 'weixin' || this.$deviceType == 'weixinh5' ? 'uniappH5' : this
						.$deviceType
				}).then(
					res => {
						uni.hideLoading();
						that.info = res.data;
						console.log(res.data)
					},
					err => {
						uni.hideLoading();
						uni.showToast({
							title: err.msg || err.response.data.msg || err.response.data.message,
							icon: "none",
							duration: 2000
						});
					}
				);
			},
			downloadIamge(imgsrc, name) {
				var that = this;
				this.isDown = true;
				var downloadUrl = imgsrc;

				if (!wx.saveImageToPhotosAlbum) {
					uni.showModal({
						title: "提示",
						content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
					});
					that.openDialogVisible = true;

					return;
				}
			},
			saveImg: function() {
				this.downloadIamge(
					this.info[this.activeIndex].wap_poster,
					"poster" + this.activeIndex
				);
			},
			downloadFile(url) {
				uni.downloadFile({
					url,
					fail: function(res) {
						uni.showModal({
							title: "提示",
							content: "保存失败"
						});
					},
					success: function(res) {
						uni.showModal({
							title: "提示",
							content: "保存成功"
						});
					}
				});
			}
		}
	};
</script>

<style lang="less">
	page {
		height: 100%;
	}

	.distribution-posters {

		height: 100%;

		.footer_btn {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 150rpx;
			display: table;
			z-index: 100;
			background-color: #FFFFFF;

			.item {
				display: table-cell;
				text-align: center;
				vertical-align: middle;
				button {
					width: 300rpx;
					display: inline-block;
					font-size: 30rpx;
					background: #FF3334;
					color: #FFFFFF;
					border-radius: 50rpx;
				}

				button:after {
					border-top-width: 0;
					border-left-width: 0;
					border-right-width: 0;
					border-bottom-width: 0;
				}
			}

		}
	}

	.banenr {
		height: 100%;
	}

	.banner swiper {
		height: 100%;
	}

	.banner .slide-image {
		width: 100%;
		height: auto;
	}
</style>
